package com.GainLoop;

import android.app.Activity;
import android.app.SearchManager;

import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.app.ListActivity;
import android.content.Intent;

import com.GainLoop.R;
import com.GainLoop.Adapters.OffersListAdapter;

public class GainList extends ListActivity  {
	ListView lv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.getgain);
        Intent intent = getIntent();
        lv=getListView();
        bindListViewToAdapter(this, lv) ;
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
          String query = intent.getStringExtra(SearchManager.QUERY);
          doMySearch(query);
        }
  	  
  	}
    protected void doMySearch(String query){
    	
    	//TODO:   gains= findGains(query);
  	 // lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, gains));
  
    }
 
    /** 
     * create the list data, and bind a custom adapter to the listview
     */
    private static void bindListViewToAdapter(Activity ctx, ListView listview) {
    	
      final OffersListAdapter listModelView = new OffersListAdapter(ctx, listview);
      Log.v("list", "binding");
      // bind a selection listener to the view
      listview.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        
    	 public void onItemSelected(AdapterView parentView, View childView, int position, long id) {
    		  Log.v("list", "item clicked.."+position);
    		  listModelView.setSelected(position);
        }
        public void onNothingSelected(AdapterView parentView) {
          listModelView.setSelected(-1);
        }
		
      });

    }
    public boolean onCreateOptionsMenu(Menu menu) {
    	Log.v("menu", "inflating menu..");
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        menu.setGroupVisible(R.id.group3, false);
        
        return true;
    }
    public boolean onOptionsItemSelected (MenuItem item){
    	Toast toast;
		 switch (item.getItemId()) {
		    case R.id.profile_button:
		    	Intent intent=new Intent(GainList.this,Profile.class);
            	GainList.this.startActivity(intent);
		        return true;
		    case R.id.help_button:
		    	toast = Toast.makeText(GainList.this,"help", Toast.LENGTH_LONG);
				toast.show();
		        return true;
		    case R.id.search_button:
		    	onSearchRequested();
		    	return true;
		    case R.id.trends_button:
		    	toast = Toast.makeText(GainList.this,"Show trends", Toast.LENGTH_LONG);
				toast.show();
				return true;
		    case R.id.addRequest_button:
		    	intent=new Intent(GainList.this,AddRequest.class);
            	GainList.this.startActivity(intent);
            	return true;
		    case R.id.requests_button:
		    	intent=new Intent(GainList.this,GiveGain.class);
		    	GainList.this.startActivity(intent);
				return true;
		    default:
		        return super.onOptionsItemSelected(item);
		    }
		
    	
    }
    static final String[] gains = new String[] {
        "gain1", "gain2", "gain3", "gain4", "gain5"
        
      };
}